NumPy Mini Project
Import the required libraries:
- Library: PIL (Pillow) for loading and saving the image.
- Library: numpy for processing the image data.
Load the image as a pixel matrix using Pillow:
- Function: Image.open("image_path")
- Convert to an array: Use np.array(image) to store pixel values in a NumPy array.
Extract the R, G, and B channels from the NumPy array:
- If the image is in RGB format, the NumPy array will have a shape of (height, width, 3), where the last dimension corresponds to the three color channels.
- Extract individual channels:
- R = image_array[:, :, 0] (Red channel)
- G = image_array[:, :, 1] (Green channel)
- B = image_array[:, :, 2] (Blue channel)
Process the pixel data to grayscale manually:
- Apply the grayscale conversion formula:
Gray=0.2989R+0.5870G+0.1140B
- Use NumPy operations to apply the formula element-wise across the image array.
Convert the processed grayscale array back to an image:
- Ensure the grayscale array is converted to uint8 type.
- Function: Image.fromarray(grayscale_array, mode="L") to convert it back to a grayscale image.
Save or display the grayscale image using Pillow:
- Function: .save("output_path") to save the image.